﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects;

namespace SodiumHydroxide
{
    public partial class AuthorView : Form
    {
        PublishingCompanyEntities publishContext;
        List<Author> authorList;
        int currentAuthorIndex = 0;

        public AuthorView()
        {
            InitializeComponent();
        }


        private void AuthorView_Load(object sender, EventArgs e)
        {
            publishContext = new PublishingCompanyEntities();
            authorList = new List<Author>();

            ObjectQuery<Author> authorQuery = publishContext.Author.Where("it.FirstName <> 'Mark'").Include("Article");
           
            authorList = authorQuery.ToList();
            
            MessageBox.Show(authorQuery.ToTraceString());
            PopulateFields();

        }

        private void PopulateFields()
        {
            Author currentAuthor = authorList[currentAuthorIndex];
            firstName.Text = currentAuthor.FirstName;
            lastName.Text = currentAuthor.LastName;
            articleCountLabel.Text = currentAuthor.Article.Count.ToString();
            authorIDLabel.Text = currentAuthor.AuthorID.ToString();
        }

        private void AuthorView_FormClosing(object sender, FormClosingEventArgs e)
        {
            publishContext.Dispose();
        }

        private void firstButton_Click(object sender, EventArgs e)
        {
            currentAuthorIndex = 0;
            PopulateFields();
        }

        private void previousButton_Click(object sender, EventArgs e)
        {
            if (currentAuthorIndex == 0)
            {
                MessageBox.Show("No previous author");
            }
            else
            {
                currentAuthorIndex -= 1;
                PopulateFields();
            }
        }

        private void nextButton_Click(object sender, EventArgs e)
        {
            if (currentAuthorIndex == authorList.Count - 1)
            {
                MessageBox.Show("No next author");
                PopulateFields();
            }
            else
            {
                currentAuthorIndex += 1;
                PopulateFields();
            }
        }

        private void lastButton_Click(object sender, EventArgs e)
        {
            currentAuthorIndex = authorList.Count - 1;
            PopulateFields();
        }

        private void update_Click(object sender, EventArgs e)
        {
            Author currentAuthor = authorList[currentAuthorIndex];
            currentAuthor.FirstName = firstName.Text;
            currentAuthor.LastName = lastName.Text;
        }

        private void sendToDB_Click(object sender, EventArgs e)
        {
            int rowsAffected = publishContext.SaveChanges(true);
            MessageBox.Show(rowsAffected.ToString() + " changes made.");
            PopulateFields();
        }

        private void clearForNew_Click(object sender, EventArgs e)
        {
            firstName.Text = string.Empty;
            lastName.Text = string.Empty;
            authorIDLabel.Text = "Not saved yet";
        }

        private void createNew_Click(object sender, EventArgs e)
        {
            Author newAuthor = new Author();
            newAuthor.FirstName = firstName.Text;
            newAuthor.LastName = lastName.Text;
            newAuthor.AuthorID = -1; //To make it obvious that it's new
            authorList.Add(newAuthor);
            publishContext.AddToAuthor(newAuthor);
            currentAuthorIndex = authorList.Count - 1;  //Set the index to the last, new item.
            PopulateFields();

        }


      
    }
}
